package com.bzframework.project.api.utils;


import com.bzframework.project.api.model.request.ApiInvokeRequest;

import java.util.Map;

/**
 * @Author CaptainBing
 * @Date 2024/8/24 20:57
 * @Description
 */
public class QueryStringUtils {

    private QueryStringUtils(){}

    public static String buildQueryString(String url, Map<String, ApiInvokeRequest.NameValueField> paramMap){

        boolean containsQuestionMark = url.contains("?");
        StringBuilder queryString = new StringBuilder();
        boolean isFirst = true;

        for (Map.Entry<String, ApiInvokeRequest.NameValueField> entry : paramMap.entrySet()) {
            ApiInvokeRequest.NameValueField nameValueField = entry.getValue();
            String name = nameValueField.getName();
            String value = nameValueField.getValue();
            if (!isFirst) {
                queryString.append("&");
            }
            queryString.append(name).append("=").append(value);
            isFirst = false;
        }
        return containsQuestionMark
                ? url + "&" + queryString
                : url + "?" + queryString;
    }

}
